using System;
using FuncWorks.Xna.UI;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace DynamicTextSizeDemo
{
	/// <summary>
	/// This is the main type for your game
	/// </summary>
	public class Game1 : Microsoft.Xna.Framework.Game
	{
		GraphicsDeviceManager graphics;
		SpriteBatch spriteBatch;
		TextStyle textStyle;
		DynamicTextSize dynamicTextSize;
		int maxWidth;
		Vector2 textPosition;
		Vector2 infoPosition;

		const string text = "FuncWorks.Xna.UI";

		public Game1()
		{
			graphics = new GraphicsDeviceManager(this);
			Content.RootDirectory = "Content";
		}

		/// <summary>
		/// Allows the game to perform any initialization it needs to before starting to run.
		/// This is where it can query for any required services and load any non-graphic
		/// related content.  Calling base.Initialize will enumerate through any components
		/// and initialize them as well.
		/// </summary>
		protected override void Initialize()
		{
			base.Initialize();
		}

		/// <summary>
		/// LoadContent will be called once per game and is the place to load
		/// all of your content.
		/// </summary>
		protected override void LoadContent()
		{
			// Create a new SpriteBatch, which can be used to draw textures.
			spriteBatch = new SpriteBatch(GraphicsDevice);

			textStyle = new TextStyle(Content.Load<SpriteFont>("SpriteFont1"), Color.White, Color.Black, 2, AnchorPoint.TopLeft);

			dynamicTextSize = new DynamicTextSize(true);
			dynamicTextSize.AddTextStyle(new TextStyle(Content.Load<SpriteFont>("SpriteFont3"), Color.White, Color.Black, 2, AnchorPoint.TopLeft));
			dynamicTextSize.AddTextStyle(new TextStyle(Content.Load<SpriteFont>("SpriteFont2"), Color.White, Color.Black, 2, AnchorPoint.TopLeft));
			dynamicTextSize.AddTextStyle(textStyle);
			maxWidth = GraphicsDevice.Viewport.TitleSafeArea.Width;
			textPosition = new Vector2(GraphicsDevice.Viewport.TitleSafeArea.Left, GraphicsDevice.Viewport.TitleSafeArea.Center.Y);
			infoPosition = new Vector2(GraphicsDevice.Viewport.TitleSafeArea.Left, GraphicsDevice.Viewport.TitleSafeArea.Top);
		}

		/// <summary>
		/// UnloadContent will be called once per game and is the place to unload
		/// all content.
		/// </summary>
		protected override void UnloadContent()
		{
		}

		/// <summary>
		/// Allows the game to run logic such as updating the world,
		/// checking for collisions, gathering input, and playing audio.
		/// </summary>
		/// <param name="gameTime">Provides a snapshot of timing values.</param>
		protected override void Update(GameTime gameTime)
		{
			GamePadState gamePad = GamePad.GetState(PlayerIndex.One);
			KeyboardState keyboard = Keyboard.GetState();

			// Allows the game to exit
			if (gamePad.Buttons.Back == ButtonState.Pressed || keyboard.IsKeyDown(Keys.Escape))
				this.Exit();

			if (gamePad.IsButtonDown(Buttons.DPadLeft) || keyboard.IsKeyDown(Keys.Left))
			{
				if (maxWidth > 0)
				{
					maxWidth--;
				}
			}
			else if (gamePad.IsButtonDown(Buttons.DPadRight) || keyboard.IsKeyDown(Keys.Right))
			{
				if (maxWidth < graphics.GraphicsDevice.Viewport.TitleSafeArea.Width)
				{
					maxWidth++;
				}

			}

			base.Update(gameTime);
		}

		/// <summary>
		/// This is called when the game should draw itself.
		/// </summary>
		/// <param name="gameTime">Provides a snapshot of timing values.</param>
		protected override void Draw(GameTime gameTime)
		{
			GraphicsDevice.Clear(Color.CornflowerBlue);

			spriteBatch.Begin();
			textStyle.DrawString(spriteBatch, String.Format("Max width: {0}", maxWidth), infoPosition);
			dynamicTextSize.GetStyleForTargetSize(text, maxWidth).DrawString(spriteBatch, text, textPosition);
			spriteBatch.End();

			base.Draw(gameTime);
		}
	}
}
